/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package br.great.functionalCore;

import java.util.Calendar;
import java.util.Date;
import java.util.Iterator;
import java.util.Vector;
import br.great.beans.Collection;
import javax.swing.*;
import br.great.config.GetCheminServer;
import br.great.clientServer.CollectionSender;
import br.great.clientServer.PhotoSender;
import br.great.DAO.PgConnect;
import br.great.beans.Address;
import br.great.beans.Photo;
import br.great.beans.User;

/**
 *
 * @author C�line DEFAUT
 */
public class SendCollection implements Runnable {
    private Collection c;
    private User user;
    private javax.swing.JLabel labelIntitule;
    private javax.swing.JLabel labelProgression;
    private javax.swing.JProgressBar progressBar;
    private ProgressBarListener listener;
    private int progress = 1;
    
    public void send(Collection c, User u, JLabel photo, JLabel progression, JProgressBar bar, ProgressBarListener listener){
        this.c = c;
        this.user = u;
        this.labelIntitule = photo;
        this.labelProgression = progression;
        this.progressBar = bar;
        this.listener = listener;
        Thread t=new Thread(this);
        t.start();
    }
    
    public void run(){
        CollectionSender collectionSender = CollectionSender.getInstance();
        Vector<Photo> p = this.c.getPhotos();
        progressBar.setMinimum(0);
        progressBar.setMaximum(p.size()+2);
        progressBar.setIndeterminate(false);
        labelIntitule.setText("collection : "+this.c.getName());
        labelProgression.setText("insertion in the database...");
        
        Date d = c.getBegin();
        Calendar cal = Calendar.getInstance();
        cal.setTime(d);
        //Calculus of Lights Status
        EarthTools_SunRiseSet esrs = new EarthTools_SunRiseSet();
//        Vector<Photo> vp = esrs.getPartOfDay_Photos(c.getPhotos());
//        c.setPhotos(esrs.getLightStatusPhotos(vp));
        
        Collection collection = collectionSender.insertCollection(c,user);
        progressBar.setValue(progress++);
        PhotoSender photoSender = PhotoSender.getInstance();
        Vector<Photo> photos = collection.getPhotos();
        
        MakePhotoRSS mpr = new MakePhotoRSS();
        String directoryOfRSS = GetCheminServer.serverfileAddress+"rssPhoto\\";
        mpr.makeFileRSS(directoryOfRSS,collection.getName(), collection.getID_Collection(),photos,user);
        
        Iterator it = photos.iterator();
        int numPhoto = 1;
        while (it.hasNext()){
            Photo photo = (Photo) it.next();
            labelIntitule.setText("photo : "+photo.getName());
            labelProgression.setText("insertion in the database...");           
            photoSender.insertPhotos(collection, photo, photo.getAddress(), user);
            //labelProgression.setText("creation of the new ontology...");
           // NewOntologyPhotoCreator ontoPhoto = new NewOntologyPhotoCreator();
           // ontoPhoto.write(collection.getName(), photo, numPhoto, user.getLogin());
            progressBar.setValue(progress++);
            numPhoto++;
        }
        NewOntologyCollectionCreator ontoCollection = new NewOntologyCollectionCreator();
        ontoCollection.write(collection, user.getLogin());
        progressBar.setValue(progress++);
        this.listener.processEnded();
        PgConnect pgConnect=PgConnect.getInstance();
        pgConnect.closeAll();
        System.out.println("envoie OK.");
    }
}
